//----------------Sonic CD Stage Select Script----------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
#alias Object.Value0	:	Object.Fade	// Also used for timing
#alias Object.Value1	:	Object.Level
#alias Object.Value2	:	Object.HoldTimer
#alias Object.Value3	:	Object.TouchButton

// States
#alias 0	:	STAGESELECT_SETUP
#alias 1	:	STAGESELECT_FADE_IN
#alias 2	:	STAGESELECT_CONTROLS
#alias 3	:	STAGESELECT_LOAD_LEVEL
#alias 4	:	STAGESELECT_EXIT

// Presentation Stages
#alias 1	:	STAGE_P_MENU

// Global SFX
#alias 27	:	SFX_G_SELECT

// Game Mode Aliases
#alias 0	:	MODE_NOSAVE

//Engine & CallBacks
#alias 2	:	MESSAGE_LOSTFOCUS

// Device Type
#alias 1	:	RETRO_MOBILE


sub ObjectMain
	switch Object.State
	case STAGESELECT_SETUP
		if Object.Fade < 8
			Object.Fade++
		else
			Object.Fade = 254
			Object.State++
		end if
		SetScreenFade(0, 0, 0, 255)
		break

	case STAGESELECT_FADE_IN
		if Object.Fade > 0
			SetScreenFade(0, 0, 0, Object.Fade)
			Object.Fade -= 8
		else
			Object.State++
		end if
		break

	case STAGESELECT_CONTROLS
		if KeyPress[0].Up == true
			Object.HoldTimer = 0
			Object.Level--
			if Object.Level < 0
				Object.Level = 89
			end if
		else
			if KeyPress[0].Down == true
				Object.HoldTimer = 0
				Object.Level++
				if Object.Level > 89
					Object.Level = 0
				end if
			end if
		end if

		if KeyDown[0].Up == true
			Object.HoldTimer++
			if Object.HoldTimer == 8
				Object.HoldTimer = 0
				Object.Level--
				if Object.Level < 0
					Object.Level = 89
				end if
			end if
		else
			if KeyDown[0].Down == true
				Object.HoldTimer++
				if Object.HoldTimer == 8
					Object.HoldTimer = 0
					Object.Level++
					if Object.Level > 89
						Object.Level = 0
					end if
				end if
			end if
		end if

		if KeyPress[0].Start == true
			Object.State = STAGESELECT_LOAD_LEVEL
		end if

		if KeyPress[0].ButtonA == true
			Object.State = STAGESELECT_LOAD_LEVEL
		end if

		if KeyPress[0].ButtonB == true
			Object.State = STAGESELECT_EXIT
		end if

#platform: Use_Origins
		if Engine.DeviceType == RETRO_MOBILE
			TempValue0 = Screen.CenterX
			TempValue0 -= 138
			TempValue1 = Screen.CenterX
			TempValue1 -= 76
			CheckTouchRect(TempValue0, 20, TempValue1, 72)
			if CheckResult > -1
				KeyDown[0].Up = true
				Object.HoldTimer++
				if Object.HoldTimer == 8
					Object.HoldTimer = 0
					Object.Level--
					if Object.Level < 0
						Object.Level = 79
					end if
				end if
			end if
			CheckTouchRect(TempValue0, 162, TempValue1, 214)
			if CheckResult > -1
				KeyDown[0].Down = true
				Object.HoldTimer++
				if Object.HoldTimer == 8
					Object.HoldTimer = 0
					Object.Level++
					if Object.Level > 79
						Object.Level = 0
					end if
				end if
			end if
			if Engine.Message == MESSAGE_LOSTFOCUS
				Object.TouchButton = 0
			end if
			TempValue0 = Screen.CenterX
			TempValue0 += 28
			TempValue1 = Screen.CenterX
			TempValue1 += 68
			CheckTouchRect(TempValue0, 162, TempValue1, 214)
			if CheckResult > -1
				Object.TouchButton = 1
			else
				CheckTouchRect(0, 0, Screen.XSize, Screen.YSize)
				if CheckResult < 0
					if Object.TouchButton == 1
						Object.TouchButton = 0
						PlaySfx(SFX_G_SELECT, false) 
						Object.State = STAGESELECT_EXIT
					end if
				else
					if Object.TouchButton == 1
						Object.TouchButton = 0
					end if
				end if
			end if
			TempValue0 += 48
			TempValue1 += 48
			CheckTouchRect(TempValue0, 162, TempValue1, 214)
			if CheckResult > -1
				Object.TouchButton = 2
			else
				CheckTouchRect(0, 0, Screen.XSize, Screen.YSize)
				if CheckResult < 0
					if Object.TouchButton == 2
						Object.TouchButton = 0
						PlaySfx(SFX_G_SELECT, false) 
						Object.State = STAGESELECT_LOAD_LEVEL
					end if
				else
					if Object.TouchButton == 2
						Object.TouchButton = 0
					end if
				end if
			end if
		end if
#endplatform

#platform: Use_Standalone
	#platform: Mobile
		TempValue0 = Screen.CenterX
		TempValue0 -= 138
		TempValue1 = Screen.CenterX
		TempValue1 -= 76
		CheckTouchRect(TempValue0, 20, TempValue1, 72)
		if CheckResult > -1
			KeyDown[0].Up = true
			Object.HoldTimer++
			if Object.HoldTimer == 8
				Object.HoldTimer = 0
				Object.Level--
				if Object.Level < 0
					Object.Level = 89
				end if
			end if
		end if

		CheckTouchRect(TempValue0, 162, TempValue1, 214)
		if CheckResult > -1
			KeyDown[0].Down = true
			Object.HoldTimer++
			if Object.HoldTimer == 8
				Object.HoldTimer = 0
				Object.Level++
				if Object.Level > 89
					Object.Level = 0
				end if
			end if
		end if

		if Engine.Message == MESSAGE_LOSTFOCUS
			Object.TouchButton = 0
		end if
		TempValue0 = Screen.CenterX
		TempValue0 += 28
		TempValue1 = Screen.CenterX
		TempValue1 += 68
		CheckTouchRect(TempValue0, 162, TempValue1, 214)
		if CheckResult > -1
			Object.TouchButton = 1
		else
			CheckTouchRect(0, 0, Screen.XSize, Screen.YSize)
			if CheckResult < 0
				if Object.TouchButton == 1
					Object.TouchButton = 0
					PlaySfx(SFX_G_SELECT, false) 
					Object.State = STAGESELECT_EXIT
				end if
			else
				if Object.TouchButton == 1
					Object.TouchButton = 0
				end if
			end if
		end if
		TempValue0 += 48
		TempValue1 += 48
		CheckTouchRect(TempValue0, 162, TempValue1, 214)
		if CheckResult > -1
			Object.TouchButton = 2
		else
			CheckTouchRect(0, 0, Screen.XSize, Screen.YSize)
			if CheckResult < 0
				if Object.TouchButton == 2
					Object.TouchButton = 0
					PlaySfx(SFX_G_SELECT, false) 
					Object.State = STAGESELECT_LOAD_LEVEL
				end if
			else
				if Object.TouchButton == 2
					Object.TouchButton = 0
				end if
			end if
		end if
	#endplatform
#endplatform
		break

	case STAGESELECT_LOAD_LEVEL
		if Object.Fade < 288
			Object.Fade += 8
		else
			Options.GameMode = MODE_NOSAVE
#platform: Mobile
			Player.Lives	 = 3
#endplatform
			Stage.ActiveList = REGULAR_STAGE
			Stage.ListPos	 = Object.Level
			LoadStage()
		end if
		TempValue0 = Object.Fade
		if TempValue0 > 255
			TempValue0 = 255
		end if
		SetScreenFade(0, 0, 0, TempValue0)
		break

	case STAGESELECT_EXIT
		if Object.Fade < 288
			Object.Fade += 8
		else
			Options.GameMode = MODE_NOSAVE
			Stage.ActiveList = PRESENTATION_STAGE
			Stage.ListPos	 = STAGE_P_MENU
			LoadStage()
		end if
		TempValue0 = Object.Fade
		if TempValue0 > 255
			TempValue0 = 255
		end if
		SetScreenFade(0, 0, 0, TempValue0)
		break

	end switch
end sub


sub ObjectDraw
	TempValue0 = 0
	while TempValue0 < 240
		TempValue1 = 0
		while TempValue1 < Screen.XSize
			DrawSpriteScreenXY(15, TempValue1, TempValue0)
			TempValue1 += 224
		loop
		TempValue0 += 48
	loop
	DrawSpriteScreenXY(16, Screen.CenterX, 80)

	TempValue0 = Screen.CenterX
	TempValue0 -= 48
	TempValue1 = Object.Level
	TempValue1 /= 10
	switch TempValue1
	case 0
		DrawSpriteScreenXY(1, TempValue0, 112)
		break
	case 1
		DrawSpriteScreenXY(2, TempValue0, 112)
		break
	case 2
		DrawSpriteScreenXY(3, TempValue0, 112)
		break
	case 3
		DrawSpriteScreenXY(4, TempValue0, 112)
		break
	case 4
		DrawSpriteScreenXY(5, TempValue0, 112)
		break
	case 5
		DrawSpriteScreenXY(6, TempValue0, 112)
		break
	case 6
		DrawSpriteScreenXY(7, TempValue0, 112)
		break
	case 7
		DrawSpriteScreenXY(8, TempValue0, 112)
		break
	case 8
		DrawSpriteScreenXY(9, TempValue0, 112)
		break
	end switch
	TempValue0 += 8
	DrawSpriteScreenXY(14, TempValue0, 112)

	TempValue0 += 8
	TempValue1 = Object.Level
	TempValue1 %= 10
	TempValue1 >>= 2
	TempValue1++
	DrawSpriteScreenXY(TempValue1, TempValue0, 112)

	TempValue0 += 8
	DrawSpriteScreenXY(14, TempValue0, 112)

	TempValue0 += 8
	TempValue1 = Object.Level
	TempValue1 %= 10
	switch TempValue1
	case 0
	case 4
		DrawSpriteScreenXY(10, TempValue0, 112)
		break

	case 1
	case 5
		DrawSpriteScreenXY(11, TempValue0, 112)
		break

	case 2
	case 6
	case 8
		DrawSpriteScreenXY(12, TempValue0, 112)
		break

	case 3
	case 7
	case 9
		DrawSpriteScreenXY(13, TempValue0, 112)
		break

	end switch
#platform: Use_Origins
	if Engine.DeviceType == RETRO_MOBILE
		TempValue0 -= 96
		if KeyDown[0].Up == true
			DrawSpriteScreenXY(18, TempValue0, 48)
		else
			DrawSpriteScreenXY(17, TempValue0, 48)
		end if
		if KeyDown[0].Down == true
			DrawSpriteScreenXY(20, TempValue0, 192)
		else
			DrawSpriteScreenXY(19, TempValue0, 192)
		end if
		TempValue0 += 160
		if Object.TouchButton == 1
			DrawSpriteScreenXY(24, TempValue0, 192)
		else
			DrawSpriteScreenXY(23, TempValue0, 192)
		end if
		TempValue0 += 48
		if Object.TouchButton == 2
			DrawSpriteScreenXY(22, TempValue0, 192)
		else
			DrawSpriteScreenXY(21, TempValue0, 192)
		end if
	end if
#endplatform

#platform: Use_Standalone
	#platform: Mobile
		TempValue0 -= 96
		if KeyDown[0].Up == true
			DrawSpriteScreenXY(18, TempValue0, 48)
		else
			DrawSpriteScreenXY(17, TempValue0, 48)
		end if

		if KeyDown[0].Down == true
			DrawSpriteScreenXY(20, TempValue0, 192)
		else
			DrawSpriteScreenXY(19, TempValue0, 192)
		end if

		TempValue0 += 160
		if Object.TouchButton == 1
			DrawSpriteScreenXY(24, TempValue0, 192)
		else
			DrawSpriteScreenXY(23, TempValue0, 192)
		end if

		TempValue0 += 48
		if Object.TouchButton == 2
			DrawSpriteScreenXY(22, TempValue0, 192)
		else
			DrawSpriteScreenXY(21, TempValue0, 192)
		end if
	#endplatform
#endplatform
end sub


sub ObjectStartup
	LoadSpriteSheet("Secrets/SecretMenus.gif")
	SpriteFrame(0, 0, 8, 16, 1, 49)				// #0  - 0
	SpriteFrame(0, 0, 8, 16, 10, 49)			// #1  - 1
	SpriteFrame(0, 0, 8, 16, 19, 49)			// #2  - 2
	SpriteFrame(0, 0, 8, 16, 28, 49)			// #3  - 3
	SpriteFrame(0, 0, 8, 16, 1, 66)				// #4  - 4
	SpriteFrame(0, 0, 8, 16, 10, 66)			// #5  - 5
	SpriteFrame(0, 0, 8, 16, 19, 66)			// #6  - 6
	SpriteFrame(0, 0, 8, 16, 28, 66)			// #7  - 7
	SpriteFrame(0, 0, 8, 16, 1, 83)				// #8  - 8
	SpriteFrame(0, 0, 8, 16, 10, 83)			// #9  - 9
	SpriteFrame(0, 0, 8, 16, 37, 49)			// #10 - A
	SpriteFrame(0, 0, 8, 16, 46, 49)			// #11 - B
	SpriteFrame(0, 0, 8, 16, 55, 49)			// #12 - C
	SpriteFrame(0, 0, 8, 16, 64, 49)			// #13 - D
	SpriteFrame(0, 0, 8, 16, 19, 83)			// #14 - "-"
	SpriteFrame(0, 0, 224, 48, 0, 0)			// #15 - Background
	SpriteFrame(-48, 0, 96, 16, 136, 66)		// #16 - Stage Select
	SpriteFrame(-10, -10, 20, 20, 136, 117)		// #17 - Up Arrow
	SpriteFrame(-10, -10, 20, 20, 136, 138)		// #18 - Up Arrow Highlight
	SpriteFrame(-10, -10, 20, 20, 157, 117)		// #19 - Down Arrow
	SpriteFrame(-10, -10, 20, 20, 157, 138)		// #20 - Down Arrow Highlight
	SpriteFrame(-20, -10, 40, 20, 159, 159)		// #21 - GO Button
	SpriteFrame(-20, -10, 40, 20, 159, 180)		// #22 - GO Button Highlight
	SpriteFrame(-20, -10, 40, 20, 200, 159)		// #23 - Exit Button
	SpriteFrame(-20, -10, 40, 20, 200, 180)		// #24 - Exit Button Highlight
end sub


// ========================
// Editor Subs
// ========================

sub RSDKDraw
	DrawSprite(0)
end sub


sub RSDKLoad
	LoadSpriteSheet("Secrets/SecretMenus.gif")
	SpriteFrame(-48, 0, 96, 16, 136, 66)		// #0 - Stage Select

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end sub
